<?php
/**
 * AbstractUploadAction class file.
 *
 * @package   controllers
 * @subpackage patientSignal
 * @since     1.0
 * @filesource
 */

/**
 * Abstract action calss which defines common class memebers thats will be used for every patient signal related
 * action class.
 * The action classes that extend from this abstract class are integrated into controller class PatientSignalController
 * @see PatientSignalController
 */
abstract class AbstractUploadAction extends CAction {

	/**
	 * The base key for upload data in session variables
	 * @var string
	 */
	const SESSION_UPLOAD_KEY='neural_patient_signal_upload';
	
	/**
	 * the key for uploaded files data in session variables
	 * @var string
	 */
	const SESSION_UPLOAD_FILES='neural_patient_signal_files';
	
	/**
	 * The key to recognize form index in session variables
	 * @var string
	 */
	const SESSION_UPLOAD_FORM='current_form';
	
	/**
	 * The key to store attributes in upload form in session variables
	 * @var string
	 */
	const SESSION_UPLOAD_ATTRIBUTES='attributes';
	
	/**
	 * The valid file name pattern of a patient signla file.
	 * @var string
	 */
	protected $filenamePattern="/^[0-9a-zA-Z\\.\\[\\]\\-_\\(\\)]+\\.smr$/";

	/**
	 * Get upload file data in session.
	 * If the data in session is not set, set an empty array in session and return the empty array.
	 * @return array session upload file info
	 */
	protected function getSessionFileInfo(){
		$session=Yii::app()->session;
		if(isset($session[self::SESSION_UPLOAD_FILES])){
			return $session[self::SESSION_UPLOAD_FILES];
		}else{
			return $session[self::SESSION_UPLOAD_FILES]=array();
		}
	}

	/**
	 * Set the "real" file name to the session file.
	 * This is used in callback methods that the upload file content 
	 * is uploaded first then the real file name from client side is updated to server.
	 * @param string $fileId the id of the session file
	 * @param string $filename the real filename to set
	 */
	protected function setSessionFileName($fileId,$filename) {
		$sessionFileInfo=$this->getSessionFileInfo();
		$sessionFile=$sessionFileInfo[$fileId];
		$sessionFileInfo[$fileId]=array(
			'path'=>$sessionFile['path'],
			'name'=>$filename,
			'time'=>$sessionFile['time'],
		);
		$this->saveSessionFileInfo($sessionFileInfo);
	}

	/**
	 * Check if the file name exists in upload files.
	 * @param string $fileName
	 * @return boolean true if the filename exists in current upload session. false otherwise.
	 */
	protected function sessionFileNameExist($fileName) {
		$sessionFileInfo=$this->getSessionFileInfo();
		foreach ($sessionFileInfo as $sessionFile){
			if($sessionFile['name']==$fileName){
				return true;
			}
		}
		return false;
	}

	/**
	 * Remove the uploaded file information in session and delete the file temporarily stored on server.
	 * @param integer $fileId
	 */
	protected function removeSessionFile($fileId) {
		$sessionFileInfo=$this->getSessionFileInfo();
		if(isset($sessionFileInfo[$fileId])){
			@unlink($sessionFileInfo[$fileId]['path']);
			unset($sessionFileInfo[$fileId]);
		}
	}

	/**
	 * Save new uploaded file information in session.
	 * @param integer $fileId
	 * @param string $filePath
	 * @param string $originalname
	 */
	protected function addSessionFile($fileId,$filePath,$originalname) {
		$sessionFileInfo=$this->getSessionFileInfo();
		$sessionFileInfo[$fileId]=array(
				'path'=>$filePath,
				'name'=>$originalname,
				'time'=>date('Y-m-d h:i'),
		);
		foreach ($sessionFileInfo as $id=>$fileInfo) {
			if(!file_exists($fileInfo['path'])){
				// clean up outdated files
				unset($sessionFileInfo[$id]);
			}
		}
		$this->saveSessionFileInfo($sessionFileInfo);
	}

	/**
	 * Save upload file information in session.
	 * @param array $sessionFileInfo
	 */
	protected function saveSessionFileInfo($sessionFileInfo) {
		$session=Yii::app()->session;
		$session[self::SESSION_UPLOAD_FILES]=$sessionFileInfo;
	}

	/**
	 * Clear upload file information in session.
	 */
	protected function clearUploadFiles() {
		unset(Yii::app()->session[self::SESSION_UPLOAD_FILES]);
	}


	/**
	 * Clear basic information of upload patient signal in session.
	 */
	protected function clearUploadForm(){
		unset(Yii::app()->session[self::SESSION_UPLOAD_KEY]);
	}

	/**
	 * Gets files uploaded in the session and returns array for user selection.
	 * @param array $sessionFileInfo The session file info. If null, the session file info will be refreshed from session
	 * @return array the files info in the session for user selection
	 */
	protected function getSessionFilesForSelection() {

		$sessionFileInfo=$this->getSessionFileInfo();
		$result=array();
		foreach ($sessionFileInfo as $id=>$fileInfo) {
			if(!file_exists($fileInfo['path'])){
				// clean up outdated files
				unset($sessionFileInfo[$id]);
			}else{
				$result[$id]=$fileInfo['name'];
			}
		}
		return $result;
	}
	
	/**
	 * Check if the given file name is valid. 
	 * @param string $filename
	 * @return 0 | 1 - file name is not valid | is valid
	 */
	protected function isLegalFileName($filename) {
		return preg_match($this->filenamePattern, $filename);
	}

	/**
	 * Return signal file name used to save file on server.
	 * @param PatientSignal $patientSignal
	 * @param string $filename
	 * @return string signal file name
	 */
	protected function createSignalFilename($patientSignal, $filename) {
		return $patientSignal->id.'_'.$filename;
	}
}